package com.weiadmin.controller.system;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeNode;
import cn.hutool.core.lang.tree.TreeUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONUtil;
import com.weiadmin.common.BaseController;
import com.weiadmin.model.system.AdminMenuModel;
import com.weiadmin.repository.MenuRepository;
import com.weiadmin.service.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 后台首页
 * @author: shizhongyu
 * @date: 2023年05月27日 17:48
 */
@RestController
@RequestMapping("/admin/index")
public class IndexController extends BaseController {
    @Autowired
    private MenuRepository menuRepository;
    @Autowired
    MenuService service;

    private static final String LINE = "----------------------------------------------------------------------";
    @GetMapping("/index")
    public ModelAndView index() {
        return display("index");
    }
    @GetMapping("/main")
    public ModelAndView main() {
        return display("main1");
    }

    @GetMapping("/log")
    public ModelAndView log() {
        return display("log");
    }
    @GetMapping("/notice")
    public ResponseEntity<?> notice()
    {
        String json = "[{\"id\":1,\"title\":\"通知\",\"children\":[{\"id\":11,\"avatar\":\"https://gw.alipayobjects.com/zos/rmsportal/ThXAXghbEsBCCSDihZxY.png\",\"title\":\"你收到了 14 份新周报\",\"context\":\"这是消息内容。\",\"form\":\"就眠仪式\",\"time\":\"刚刚\"},{\"id\":12,\"avatar\":\"https://gw.alipayobjects.com/zos/rmsportal/OKJXDXrmkNshAMvwtvhu.png\",\"title\":\"曲妮妮 已通过第三轮面试\",\"context\":\"这是消息内容。\",\"form\":\"就眠仪式\",\"time\":\"刚刚\"},{\"id\":11,\"avatar\":\"https://gw.alipayobjects.com/zos/rmsportal/kISTdvpyTAhtGxpovNWd.png\",\"title\":\"可以区分多种通知类型\",\"context\":\"这是消息内容。\",\"form\":\"就眠仪式\",\"time\":\"刚刚\"},{\"id\":12,\"avatar\":\"https://gw.alipayobjects.com/zos/rmsportal/GvqBnKhFgObvnSGkDsje.png\",\"title\":\"左侧图标用于区分不同的类型\",\"context\":\"这是消息内容。\",\"form\":\"就眠仪式\",\"time\":\"刚刚\"},{\"id\":11,\"avatar\":\"https://gw.alipayobjects.com/zos/rmsportal/ThXAXghbEsBCCSDihZxY.png\",\"title\":\"内容不要超过两行字\",\"context\":\"这是消息内容。\",\"form\":\"就眠仪式\",\"time\":\"刚刚\"}]},{\"id\":2,\"title\":\"消息\",\"children\":[{\"id\":11,\"avatar\":\"https://gw.alipayobjects.com/zos/rmsportal/ThXAXghbEsBCCSDihZxY.png\",\"title\":\"你收到了 14 份新周报\",\"context\":\"这是消息内容。\",\"form\":\"就眠仪式\",\"time\":\"刚刚\"},{\"id\":12,\"avatar\":\"https://gw.alipayobjects.com/zos/rmsportal/OKJXDXrmkNshAMvwtvhu.png\",\"title\":\"曲妮妮 已通过第三轮面试\",\"context\":\"这是消息内容。\",\"form\":\"就眠仪式\",\"time\":\"刚刚\"},{\"id\":11,\"avatar\":\"https://gw.alipayobjects.com/zos/rmsportal/kISTdvpyTAhtGxpovNWd.png\",\"title\":\"可以区分多种通知类型\",\"context\":\"这是消息内容。\",\"form\":\"就眠仪式\",\"time\":\"刚刚\"},{\"id\":12,\"avatar\":\"https://gw.alipayobjects.com/zos/rmsportal/GvqBnKhFgObvnSGkDsje.png\",\"title\":\"左侧图标用于区分不同的类型\",\"context\":\"这是消息内容。\",\"form\":\"就眠仪式\",\"time\":\"刚刚\"},{\"id\":11,\"avatar\":\"https://gw.alipayobjects.com/zos/rmsportal/ThXAXghbEsBCCSDihZxY.png\",\"title\":\"内容不要超过两行字\",\"context\":\"这是消息内容。\",\"form\":\"就眠仪式\",\"time\":\"刚刚\"}]},{\"id\":3,\"title\":\"代办\",\"children\":[]}]";
        JSONArray jsonObject = JSONUtil.parseArray(json);
        return success("请求成功",jsonObject);
    }
    @GetMapping("/getAdminMenus")
    public ResponseEntity<?> getMenus()
    {
        List<TreeNode<String>> nodeList = CollUtil.newArrayList();
        List<AdminMenuModel> menuList =  service.getAll();
        for (AdminMenuModel item: menuList) {
            TreeNode<String> node = new TreeNode<>(item.getId(), item.getParentId(), item.getTitle(), item.getId());
            Map<String, Object> map = new HashMap<>();

            node.setId(item.getId());
            node.setName(item.getTitle());
            node.setWeight(item.getId());
            map.put("title",item.getTitle());
            map.put("icon",item.getIcon());
            map.put("href",item.getHref());
            map.put("type",item.getType());

            node.setExtra(map);

            nodeList.add(node);
        }
        List<Tree<String>> treeList = TreeUtil.build(nodeList, "0");
        return success("请求成功",treeList);
    }

    public String getMenuJson()
    {
        String json = "[{\"id\":\"system\",\"title\":\"系统管理\",\"icon\":\"layui-icon layui-icon-set-fill\",\"type\":0,\"href\":\"\",\"children\":[{\"id\":601,\"title\":\"用户管理\",\"icon\":\"layui-icon layui-icon-face-smile\",\"type\":1,\"openType\":\"_iframe\",\"href\":\"view/system/user.html\"},{\"id\":602,\"title\":\"系统设置\",\"icon\":\"layui-icon layui-icon-face-cry\",\"type\":1,\"openType\":\"_iframe\",\"href\":\"/admin/setting/index\"},{\"id\":603,\"title\":\"权限管理\",\"icon\":\"layui-icon layui-icon-face-cry\",\"type\":1,\"openType\":\"_iframe\",\"href\":\"view/system/power.html\"},{\"id\":604,\"title\":\"部门管理\",\"icon\":\"layui-icon layui-icon-face-cry\",\"type\":1,\"openType\":\"_iframe\",\"href\":\"view/system/deptment.html\"},{\"id\":605,\"title\":\"行为日志\",\"icon\":\"layui-icon layui-icon-face-cry\",\"type\":1,\"openType\":\"_iframe\",\"href\":\"view/system/log.html\"},{\"id\":606,\"title\":\"数据字典\",\"icon\":\"layui-icon layui-icon-face-cry\",\"type\":1,\"openType\":\"_iframe\",\"href\":\"view/system/dict.html\"}]}]";
       return json;
    }
}
